package com.quyou.funtour.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.quyou.funtour.bean.ResultInfo;
import com.quyou.funtour.dao.RegionDao;
import com.quyou.funtour.enummmeration.ActivityStatus;
import com.quyou.funtour.po.ActivityPo;
import com.quyou.funtour.po.RegionPo;
import com.quyou.funtour.po.UserParticipateActivityPo;
import com.quyou.funtour.po.UserPo;
import com.quyou.funtour.service.ActivityService;
import com.quyou.funtour.service.RegionService;
import com.quyou.funtour.service.UserParticipateActivityService;
import com.quyou.funtour.vo.ActivityVo;
import com.quyou.funtour.vo.RegionVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 活动接口
 */
@Controller
@RequestMapping("/activity")
@CrossOrigin
public class ActivityController {

    @Autowired
    private ActivityService activityService;
    @Autowired
    private RegionDao regionDao;
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;
    @Autowired
    private RegionService regionService;
    @Autowired
    private UserParticipateActivityService userParticipateActivityService;


    /**
     * 根据活动id获取活动
     * @param id 活动id
     * @return 结果对象
     */
    @RequestMapping("/getActivityById")
    @ResponseBody
    public ResultInfo getActivityById(String id){
        //浏览记录
        //UserPo loginUser = (UserPo) session.getAttribute("login_user");
        //UserPo o = (UserPo) redisTemplate.opsForValue().get(rdSession);
        ActivityPo activityById = activityService.getActivityById(id);
        return ResultInfo.ok(activityById);
    }

    /**
     * 用户预约活动
     * @param userParticipateActivityPo 用户预约信息
     * @return 结果对象
     */
    @RequestMapping("/makeAnAppointment")
    @ResponseBody
    public ResultInfo makeAnAppointment
    (
            @RequestHeader("3rd_session")String rdSession,
            UserParticipateActivityPo userParticipateActivityPo
    )
    {
        UserPo o = (UserPo) redisTemplate.opsForValue().get(rdSession);
        //String attribute = (String) session.getAttribute("3rd_session");
        if(o==null){
            return ResultInfo.no("您还未登录");
        }
        return activityService.makeAnAppointment(userParticipateActivityPo,o);
    }

    /**
     * 获取用户已经预约的活动
     * @return
     */
    @RequestMapping("/getyuyueIngActivity")
    @ResponseBody
    public ResultInfo getyuyueIngActivity
    (
            @RequestHeader("3rd_session")String rdSession
    )
    {
        UserPo o = (UserPo) redisTemplate.opsForValue().get(rdSession);
        if(o==null){
            return ResultInfo.no("获取失败，请重新登录");
        }

        //获取预约的活动
        List<ActivityVo> activityVoList = activityService.getyuyueIngActivity(o.getId());

        return ResultInfo.ok(activityVoList);
    }

    /**
     * 获取我参与的活动
     * @return
     */
    @RequestMapping("/getParticipateActivity")
    @ResponseBody
    public ResultInfo getParticipateActivity
    (
            @RequestHeader("3rd_session")String rdSession
    )
    {
        UserPo o = (UserPo) redisTemplate.opsForValue().get(rdSession);
        //String attribute = (String) session.getAttribute("3rd_session");
        if(o==null){
            return ResultInfo.no("获取失败，请重新登录");
        }
        //获取预约的活动
        List<ActivityVo> activityVoList = activityService.getParticipateActivity(o.getId());
        return ResultInfo.ok(activityVoList);
    }


    /**
     * 根据活动id获取活动数据
     * @param id 活动id
     * @return 结果对象
     */
    @GetMapping("/getById/{aid}")
    @ResponseBody
    public ResultInfo getById(@PathVariable("aid") String id){
        ActivityPo regionPo = activityService.getById(id);
        RegionPo nameById = regionDao.getNameById(regionPo.getRid());
        RegionVo regionVo = new RegionVo();
        BeanUtils.copyProperties(nameById,regionVo);
        regionPo.setRname(regionVo);
        if(regionPo==null){
            return ResultInfo.no("未找到此活动");
        }
        return ResultInfo.ok(regionPo);
    }

    /**
     * 根据活动id获取活动图片
     * @param id
     * @return
     */
    @GetMapping("/getActivityImgById/{aid}")
    @ResponseBody
    public ResultInfo getActivityImgById(@PathVariable("aid") String id){
        List<String> activityImgById = activityService.getActivityImgById(id);
        return ResultInfo.ok(activityImgById);
    }

    /**
     * 获取活动分页集合
     * @param current 当前页
     * @param size 每页显示条数
     * @return
     */
    @GetMapping("/ctivityList")
    @ResponseBody
    public ResultInfo ctivityList(@RequestParam("current") Integer current,@RequestParam("size") Integer size){
        Page<ActivityPo> activityPoPage = activityService.ctivityList(current, size);
        return ResultInfo.ok(activityPoPage);
    }

    /**
     * 添加活动
     * @param activity 活动
     * @return
     */
    @GetMapping("/addActivity")
    @ResponseBody
    public ResultInfo addActivity(ActivityPo activity){

        boolean save = activityService.addActivity(activity);
        if(save){
            return ResultInfo.ok("保存成功");
        }else {
            return ResultInfo.no("保存失败");
        }
    }

    /**
     * 修改活动
     * @param activity 活动对象
     * @return
     */
    @GetMapping("/updateActivity")
    @ResponseBody
    public ResultInfo updateActivity(ActivityPo activity){
        boolean b = activityService.updateById(activity);
        if(b){
            return ResultInfo.ok("修改成功");
        }else {
            return ResultInfo.no("修改失败");
        }
    }

    /**
     * 删除活动
     * @param id 活动id
     * @return 结果对象
     */
    @GetMapping("/deleteActivity/{aid}")
    @ResponseBody
    public ResultInfo deleteActivity(@PathVariable("aid") String id){
        boolean b = activityService.removeById(id);
        if(b){
            return ResultInfo.ok("删除成功");
        }else {
            return ResultInfo.no("删除失败");
        }
    }

    /**
     * 获取正在组队滴所有活动
     * @return
     */
    @GetMapping("/getActivity")
    @ResponseBody
    public ResultInfo getActivity(){
        List<ActivityPo> list = activityService.list();
        List<ActivityVo> activityVoList = new ArrayList<>();
        for (ActivityPo activityPo : list) {
            if(activityPo.getOrderTime().getTime()>new Date().getTime()
                    && activityPo.getStartTime().getTime()>new Date().getTime()){
                ActivityVo activityVo = new ActivityVo();
                BeanUtils.copyProperties(activityPo,activityVo);
                List<UserParticipateActivityPo> a_id = userParticipateActivityService.list(new QueryWrapper<UserParticipateActivityPo>().eq("a_id", activityPo.getId()));
                //设置开始时间
                activityVo.setEndTime(activityPo.getStartTime());
                //设置活动预约人数
                activityVo.setAlreadyPeopleNum(a_id.size());
                activityVoList.add(activityVo);
            }
        }
        return ResultInfo.ok(activityVoList);
    }

    /**
     * 模糊查询活动
     * @param current 当前页
     * @param size 每页显示条数
     * @return
     */
    @GetMapping("/likeActivity")
    @ResponseBody
    public ResultInfo likeActivity(@RequestParam("current") Integer current,
                                 @RequestParam("size") Integer size,
                                 ActivityPo activity){
        //构建分页对象
        Page<ActivityPo> userPoPage = new Page<>(current,size);
        //构建条件
        QueryWrapper<ActivityPo> queryWrapper = new QueryWrapper<ActivityPo>();
        if(activity.getName()!=null && !activity.getName().equals("")){
            if(activity.getStatus()!=null){
                queryWrapper
                        .like("name", activity.getName()).or()
                        .like("address", activity.getName()).or()
                        .like("principal", activity.getName()).or()
                        .eq("status", activity.getStatus()).or()
                        .eq("r_id",activity.getRid()).or(w->w
                        .ge("start_time",activity.getStartTime()).and(we->we
                                .le("end_time",activity.getEndTime())));
            }
            queryWrapper
                    .like("name", activity.getName()).or()
                    .like("address", activity.getName()).or()
                    .like("principal", activity.getName()).or()
                    .eq("r_id",activity.getRid()).or(w->w
                    .ge("start_time",activity.getStartTime()).and(we->we
                            .le("end_time",activity.getEndTime())));
        }else {
            queryWrapper
                    .eq("status", activity.getStatus()).or()
                    .eq("r_id",activity.getRid()).or(w->w
                    .ge("start_time",activity.getStartTime()).and(we->we
                            .le("end_time",activity.getEndTime())));
        }

        //查询专区
        Page<ActivityPo> page = activityService.page(userPoPage, queryWrapper);
        for (ActivityPo record : page.getRecords()) {
            RegionPo nameById = regionDao.getNameById(record.getRid());
            RegionVo regionVo = new RegionVo();
            BeanUtils.copyProperties(nameById,regionVo);
            record.setRname(regionVo);
        }
        return ResultInfo.ok(page);
    }

    /**
     * 根据状态获取活动
     * @param status 状态
     * @return
     */
    @GetMapping("/getRegionByStatus")
    @ResponseBody
    public ResultInfo getActivityByStatus(@RequestParam("current") Integer current,
                                        @RequestParam("size") Integer size,
                                        @RequestParam("status") ActivityStatus status){
        Page<ActivityPo> userPoPage = new Page<>(current,size);
        QueryWrapper<ActivityPo> queryWrapper = new QueryWrapper<ActivityPo>()
                .eq("status",status);
        Page<ActivityPo> page = activityService.page(userPoPage, queryWrapper);
        return ResultInfo.ok(page);
    }

}
